﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frminshops : Form
    {
        DataSet ds;
        int id=0;
        public frminshops(int myid)
        {
            id = myid;
            InitializeComponent();
        }
        private void bind()
        {
            dgvin.AutoGenerateColumns = false;
            string sql;
            sql = "SELECT useinfo.[UID], useinfo.[unum], useinfo.[udate], ProductInfo.PName FROM useinfo INNER JOIN ProductInfo ON useinfo.pid = ProductInfo.PID  where cid=" + id;
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and udate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "大于")
            {
                sql += string.Format(" and udate>#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "小于")
            {
                sql += string.Format(" and udate<#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "等于")
            {
                sql += string.Format(" and udate =#{0}#", dtps.Value.ToShortDateString());
            }
            if (ddldate.Text == "之间")
            {
                sql += string.Format(" and udate>#{0}# and udate<#{1}#", dtps.Value.ToShortDateString(), dtpe.Value.ToShortDateString());
            }
            sql += " order by udate desc";
            ds = DbHelperOleDb.Query(sql);                   
            dgvin.DataSource = ds.Tables[0];
            int widths = 0;
            for (int i = 0; i < dgvin.Columns.Count; i++)
            {
                dgvin.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader);  // 自动调整列宽  
                widths += dgvin.Columns[i].Width;   // 计算调整列后单元列的宽度和                       
            }
            if (widths >= dgvin.Size.Width)  // 如果调整列的宽度大于设定列宽  
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;  // 调整列的模式 自动  
            else
                dgvin.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  // 如果小于 则填充 
        }

        private void ddldate_SelectedIndexChanged(object sender, EventArgs e)
        {            
            if (ddldate.Text == "之间")
            {
                dtps.Visible = true;
                dtpe.Visible = true;
            }
            else if (ddldate.Text == "所有")
            {
                dtps.Visible = false;
                dtpe.Visible = false;
            }
            else
            {
                dtps.Visible = true;
                dtpe.Visible = false;
            }        
        }

        private void frminshops_Load(object sender, EventArgs e)
        {
            ddldate.Text = "所有";
            dtps.Value = DateTime.Now;
            dtpe.Value = DateTime.Now;
            bind();
        }

        private void btnOUT_Click(object sender, EventArgs e)
        {
            myfun.DataToExcel(dgvin);
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvin);
            m.Print();
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            frminshop f = new frminshop(id);
            f.ShowDialog();
            bind();
        }
    }
}
